//
//  HomeController.swift
//  SinaWeibo
//
//  Created by apple on 15/11/8.
//  Copyright © 2015年 apple. All rights reserved.
//
/*
App Key：1831572088
App Secret：14bdd2aab9424167dce6ec5d1b846a13

*/
import UIKit
import AFNetworking
import SVProgressHUD

private let HomeCellID = "HomeCellID"

class HomeController:  BaseController{
    
    //定义模型数组
    private lazy var statueses = [Statues]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !userLogin {
            
            visitorLoginView?.setUPVisitor(nil, title: "关注一些人.回这里看看有什么惊喜")
            return
        }
        
        
        loadData()
        
        registerCell()
        
    }
    
    /**
    注册单元格
    */
    
    private func registerCell () {
        
        //下拉刷新
        refreshControl = UIRefreshControl()
        
        //监听方法
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
        
        self.tableView.registerClass(StatusCell.self, forCellReuseIdentifier: HomeCellID)
        
        
        tableView.rowHeight = 200
        
        //IOS8 以后 tableView 自动计算行高 需要指定两个属性
        //但是必须指定一个自上而下的约束
        
        tableView.estimatedRowHeight = 200
        
        tableView.rowHeight = UITableViewAutomaticDimension
        
        //设置底部视图为加载更多的小菊花
        tableView.tableFooterView = indicatorView
        
    }
    
    @objc  private func loadData() {
        
        var since_id = 0
        
        var max_id = 0
        
        if indicatorView.isAnimating() {
            
            //上拉加载更多
            
            since_id = 0
            max_id = statueses.last?.id ?? 0
            
        } else {
            
            //下拉刷新
            max_id = 0
            since_id = statueses.first?.id ?? 0
        }
        
        
        StatusList.loadData(since_id, max_id: max_id) { (array) -> () in
            
            //print(array?.count)
            //停止刷新
            self.refreshControl?.endRefreshing()
            if array == nil {
                //请求失败
                SVProgressHUD.showErrorWithStatus("您暂未登陆")
                //停止动画
                self.indicatorView.stopAnimating()
                return
            }
            //            //一定拿到数据  赋值]
            //            self.statueses = array!
            
            if since_id > 0 {
                
                //下拉刷新
                self.statueses = array! + self.statueses
                
            } else if max_id > 0 {
                
                //上拉加载更多
                self.statueses += array!
                //数据加载完成就停止动画
                self.indicatorView.stopAnimating()
                
            } else {
                
                //首次加载
                self.statueses = array!
            }
            
            //刷新表格
            self.tableView.reloadData()
        }
    }
    
    //懒加载上拉加载更多的小菊花
    private lazy var indicatorView :UIActivityIndicatorView = {
        
        let indicator = UIActivityIndicatorView(activityIndicatorStyle:.Gray)
        
        return indicator
        
        }()
}

/**
*  实现数据源方法
*/
extension HomeController {
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return self.statueses.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellID, forIndexPath: indexPath) as? StatusCell
        
        let status  = statueses[indexPath.row]
        cell!.status = status
        //静默加载
        if indexPath.row == statueses.count - 1 && !indicatorView.isAnimating(){
            //倒数第二行 加载更多数据  并且小菊花没有转动的时候 再加载更多数据
            indicatorView.startAnimating()
            loadData()
        }
        
        return cell!
    }
    
}